package com.ddpt.platform.common.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 获取一周时间
 * @author QSW
 * @data 2022/6/14 17:41
 **/
public class WeekUtils {


    public static void main(String[] args) {
        //GetCurrentWeekAllDate();
        List<String> list = GetSomedayWeekAllDate("2022-6-13");
        System.out.println(list);
    }


    // 获取当前一周的一星期的日期
    public static void GetCurrentWeekAllDate() {

        // 获取当天的毫秒时间
        long currentTimeMillis = System.currentTimeMillis();

        List<String> list = getAllWeekDayDateByMillis(currentTimeMillis);

        for (String date : list) {

            System.out.println("date : " + date);
        }
    }

    // 获取指定任意日期当周的一星期的日期
    public static List<String> GetSomedayWeekAllDate(String date) {

        // 指定天的毫秒时间
        long currentTimeMillis = getWhatDaySomeDateMillis(date);

        List<String> list = getAllWeekDayDateByMillis(currentTimeMillis);

        return list;
    }

    // someDataStr 格式："yyyy-MM-dd"
    // 返回 时间毫秒
    private static long getWhatDaySomeDateMillis(String someDataStr) {

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(someDataStr);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1;
    }

    // 获取指定日期毫秒时间得到 星期几
    private static String getWhatDaySomeday(long timeMillis) {

        Date toDayDate = new Date(timeMillis);
        SimpleDateFormat formatE = new SimpleDateFormat("E");
        String week = null;
        try {
            week = formatE.format(toDayDate);
            System.out.println(week);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return week;
    }

    // 根据 星期几到获取与 星期一 相差几天
    private static int getHowManyDayFromMonday(String someDay) {
        int day = 0;
        if (someDay.equals("星期一")||"Monday".contains(someDay)){
            day = 0;
        }else if (someDay.equals("星期二")||"Tuesday".contains(someDay)){
            day = 1;
        }else if (someDay.equals("星期三")||"Wednesday".contains(someDay)){
            day = 2;
        }else if (someDay.equals("星期四")||"Thursday".contains(someDay)){
            day = 3;
        }else if (someDay.equals("星期五")||"Friday".contains(someDay)){
            day = 4;
        }else if (someDay.equals("星期六")||"Saturday".contains(someDay)){
            day = 5;
        }else if (someDay.equals("星期日")||"Sunday".contains(someDay)){
            day = 6;
        }else {
            day = 0;
        }
        return day;
    }

    // 获取指定日期毫秒时间的当周一星期的日期
    private static List<String> getAllWeekDayDateByMillis(long timeMills) {
        List<String> list = new ArrayList<String>();

        // 得到指定时间是周几
        String week = getWhatDaySomeday(timeMills);

        // 记录与周一的间隔天数
        int dayFromMonday = getHowManyDayFromMonday(week);

        // 获取这周第一天毫秒值
        long dayMillis = 24 * 60 * 60 * 1000;
        // 获取这周第一天的日子
        long firstOfWeekMillis = timeMills - dayFromMonday * dayMillis;

        // 使用 for 循环得到当前一周的日子（7天的日子）
        for (long i = firstOfWeekMillis; i < firstOfWeekMillis + 7 * dayMillis; i += dayMillis) {
            Date targetDate = new Date(i);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String targetDay = format.format(targetDate);
            list.add(targetDay);
        }

        return list;
    }

}
